package br.com.informatos.localizador;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

	private static final String TAG = "MainActivity";

	private TextView latitudeText;
	private TextView logitudeText;
	private LocationManager locationmanager;
	private String provider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		latitudeText = (TextView) findViewById(R.id.txt_latitude);
		logitudeText = (TextView)findViewById(R.id.txt_longitude);

		locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		Criteria criteria = new Criteria();
		provider = locationmanager.getBestProvider(criteria, false);

		Location location = locationmanager.getLastKnownLocation(provider);

		if(location != null){
			Log.d(TAG, "provider" + provider + "foi selecionado.");
			onLocationChanged(location);

		}else{
			latitudeText.setText(R.string.location_not_avaliable);
			logitudeText.setText(R.string.location_not_avaliable);
		}

	}


	@Override
	protected void onResume() {
		super.onResume();
		locationmanager.requestLocationUpdates(provider, 400, 1, this);
	}


	@Override
	protected void onPause() {	
		super.onPause();
		locationmanager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(Location location) {
		double lat = location.getAltitude();
		double lng = location.getLongitude();
		latitudeText.setText(getString(R.string.point_label, lat));
		logitudeText.setText(getString(R.string.point_label, lng));
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(this, "Novo Provider" + provider, Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(this, "Provider Desabilitado" + provider, Toast.LENGTH_SHORT).show();

	}

}
